/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.relatorio;

import br.com.deployment.daoImpl.QuestionnaireResponseDAOImpl;
import br.com.deployment.entities.QuestionnaireResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerRelEdit {

    @EJB
    QuestionnaireResponseDAOImpl qService;
    
    // Lista Questionaire
    private List<QuestionnaireResponse> lstQuestionnaireResponse;
    
    // Lista Questionáirios Anteriores 
    private List<QuestionnaireResponse> lstQuestionnairePreviewDate;
   
    
    public ManagerRelEdit() {
    }

    public void generetorReportPdf(QuestionnaireResponse quesResponse) throws JRException, IOException {

        HashMap<String, Object> h = new HashMap<String, Object>();
        String text = "";
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date data = quesResponse.getDateQ(); 
          
        text +="O(A)"+quesResponse.getExecutor().getFunction()+", "+quesResponse.getExecutor().getName()+", "
                +quesResponse.getExecutor().getFunction()+" "+quesResponse.getExecutor().getOrgan().getName()+
                " faz saber ao delegado do "+quesResponse.getNotaryOffice().getNome()+" na cidade de "+quesResponse.getNotaryOffice()
                .getAddress().getCity().getName()+" - "+quesResponse.getNotaryOffice().getAddress().getCity().getStates().getSgl()+" que na data "+
                df.format(data) + ", realizará inspeção correicional na serventia. ";
                 
            
        h.put("data", new Date());
        h.put("text", text);
        lstQuestionnaireResponse = qService.findQuestionnaireResponse(quesResponse);
        JRBeanCollectionDataSource collecion = new JRBeanCollectionDataSource(lstQuestionnaireResponse);
        String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/relatorio/relEdictNextEyre.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, h, collecion);
        HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=edital.pdf");
        ServletOutputStream serveOutputStream = httpServletResponse.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, serveOutputStream);
        FacesContext.getCurrentInstance().responseComplete();

    }

   
}
